<?php
namespace Modules;
use Admin\Models\coop\XhCoopInfo;
use Admin\Models\ims\XHImsProduce;
use Helpers\Main;
use Helpers\XhException;

class Coop {

    /**
     * 获取供应商供应的商品
     *
     * @param array $coopIds
     * @param bool $withName
     *
     *  @return array|bool|mixed|null
     * @throws XhException
     * create by dale  20180821
     */
    public static function getProduce($coopIds=[], $withName=false)
    {
        try {
            if (!$coopIds) {
                return [];
            }
            $coopArr = XhCoopInfo::find([
                'columns'    => 'sProdIds',
                'conditions' => 'bDelete = 0 and iCIId in ({ids:array})',
                'bind'       => ['ids' => $coopIds]
            ])->toArray();
            $coopArr = array_column($coopArr, 'sProdIds');
            $prodArr = [];
            foreach ($coopArr as $coop) {
                if ($coop) {
                    $prods = explode(',', $coop);
                    $prodArr = array_merge($prodArr, $prods);
                }
            }
            $prodArr   = array_values(array_filter(array_unique($prodArr)));
            if (!$withName) {
                return $prodArr;
            }
            $prodEnum = XHImsProduce::getProds();
            foreach ($prodEnum as $index => $prodName) {
                if (!in_array($index, $prodArr)) {
                    unset($prodEnum[$index]);
                }
            }
            return $prodEnum;
        } catch (\Exception $e) {
            throw new XhException('获取产品失败', $e->getMessage());
        }
    }

    /**
     * 获取合作商扩展权限
     *
     * @param $target
     *
     * @return array|bool|mixed
     * @throws XhException
     *
     * create by dale  20180822
     */
    public static function getPower($target)
    {
        return Power::getExtraPower('coop', $target);
    }




}